package testGroovy;

import groovy.json.JsonBuilder;

def GETLIST_BOOLEAN = { insurancePackage, entry ->
    insurancePackage[entry.key]  ? entry.value : null
}

def GETLIST_DOUBLE = { insurancePackage, entry ->
    insurancePackage[entry.key] > 0 ? entry.value : null
}


def insurancePackage = [
        thirdPartyAmount: 100000,
        thirdPartyIop   : true,
        damage          : false,
        damageIop       : false,
        theft           : false,
        theftIop        : false,
        engine          : false,
        engineIop       : false,
        glass           : false,
        glassType       : 0,
        driverAmount    : 0,
        driverIop       : true,
        passengerAmount : 0,
        passengerIop    : false,
        spontaneousLoss : false,
        scratchAmount   : 0,
        scratchIop      : false,
]

def damage = 388000
def theft = 353080
def thirdParty = 100000
def scratch = 2000
def driver = 10000
def passenger = 10000
def glassType = 1

//def damage = vehicleInfo.purchasePrice
//def theft = context.theftCoverage
//def thirdParty = insurancePackage.thirdPartyAmount
//def scratch = insurancePackage.scratchAmount
//def driver = insurancePackage.passengerAmount
//def passenger = insurancePackage.driverAmount
//def glassType = insurancePackage.glassType.id

def insurancePackage_double = [
        thirdPartyAmount: ['第三者责任险', 'ThirdPartyLiabilityCoverage', "${thirdParty}"],
        driverAmount    : ['车上司机责任险', 'InCarDriverLiabilityCoverage', "${driver}"],
        passengerAmount : ['车上乘客责任险', 'InCarPassengerLiabilityCoverage', "${passenger}"],
        scratchAmount   : ['车身油漆单独损伤险', 'CarBodyPaintCoverage', "${scratch}"],
]

def insurancePackage_boolean = [
        thirdPartyIop   : ['第三者责任险不计免赔', 'ThirdPartyLiabilityExemptDeductibleSpecialClause', ""],
        damageIop       : ['车损险不计免赔', 'DamageLossExemptDeductibleSpecialClause', ""],
        theftIop        : ['全车盗抢险不计免赔', 'TheftCoverageExemptDeductibleSpecialClause', ""],
        driverIop       : ['车上人员责任险不计免赔', 'InCarPersonLiabilityExemptDeductibleSpecialClause', ""],
        passengerIop    : ['车上人员责任险不计免赔', 'InCarPersonLiabilityExemptDeductibleSpecialClause', ""],
        engineIop       : ['附加险不计免赔', 'RiderExemptDeductibleSpecialClause', ""],
        scratchIop      : ['附加险不计免赔', 'RiderExemptDeductibleSpecialClause', ""],
]

def insurancePackage_glass = [
        glass           : ['玻璃破碎险', 'GlassBrokenCoverage', "${glassType}"],
]


def insurancePackage_boolean2Double = [
        damage          : ['车辆损失险', 'DamageLossCoverage', "${damage}"],
        theft           : ['全车盗抢损失险', 'TheftCoverage', "${theft}"],
        engine          : ['涉水损失险', 'PaddleDamageCoverage', "${theft}"],
        spontaneousLoss : ['自燃损失险', 'SelfIgniteCoverage', "${theft}"],
]

def _MAPPING_COVERAGEINFO = insurancePackage_double.collect(GETLIST_DOUBLE.curry(insurancePackage)) - null
_MAPPING_COVERAGEINFO += insurancePackage_boolean.collect(GETLIST_BOOLEAN.curry(insurancePackage)) - null
_MAPPING_COVERAGEINFO += insurancePackage_glass.collect(GETLIST_BOOLEAN.curry(insurancePackage)) - null
_MAPPING_COVERAGEINFO += insurancePackage_boolean2Double.collect(GETLIST_BOOLEAN.curry(insurancePackage)) - null

def list = _MAPPING_COVERAGEINFO.collect { coverageName, coverageCode,coverageAmount ->
    [
            "coverageCode": coverageCode,
            "coverageName": coverageName,
            "coverageAmount": coverageAmount
    ]
}
JsonBuilder builder = new JsonBuilder(list)
builder.toString()
println builder.toString()